package com.smart.village.big.screen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.smart.village.big.screen.mapper.AdminChartsMapper;
import com.smart.village.big.screen.service.AdminChartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
public class AdminChartsServiceImpl implements AdminChartsService {

    @Autowired
    private AdminChartsMapper adminChartsMapper;

    @Override
    public JSONObject top3() {
        JSONObject jsonObject = new JSONObject();
        String zongDingDanLiang = "0";
        String shangYueBiJiaoDingDanLiang = "0";
        List<JSONObject> totalCheckInStatics0 = adminChartsMapper.totalCheckInStatics(0);
        if (totalCheckInStatics0.size() > 0) {
            zongDingDanLiang = totalCheckInStatics0.get(0).getString("cnt");
        }
        List<JSONObject> totalCheckInStatics1 = adminChartsMapper.totalCheckInStatics(1);
        if (totalCheckInStatics1.size() > 0) {
            shangYueBiJiaoDingDanLiang = Integer.parseInt(zongDingDanLiang) - Integer.parseInt(totalCheckInStatics1.get(0).getString("cnt")) + "";
        }
        jsonObject.put("zongDingDanLiang", zongDingDanLiang);
        jsonObject.put("shangYueBiJiaoDingDanLiang", shangYueBiJiaoDingDanLiang);

        String ruZhuLv = "0";
        String shangYueBiJiaoRuZhuLv = "0";
        String zongShouRu = "0";
        String shangYueBiJiaoZongShouRu = "0";

        List<JSONObject> checkInPercentStatics0 = adminChartsMapper.checkInPercentStatics(0);
        if (checkInPercentStatics0.size() > 0) {
            ruZhuLv = checkInPercentStatics0.get(0).getString("checkPercent");
        }

        List<JSONObject> checkInPercentStatics1 = adminChartsMapper.checkInPercentStatics(1);
        if (checkInPercentStatics1.size() > 0) {
            shangYueBiJiaoRuZhuLv = Float.parseFloat(ruZhuLv) - checkInPercentStatics1.get(0).getFloat("checkPercent") + "";
        }
        jsonObject.put("ruZhuLv", ruZhuLv);
        jsonObject.put("shangYueBiJiaoRuZhuLv", shangYueBiJiaoRuZhuLv);

        List<JSONObject> checkInPlanPriceStatics0 = adminChartsMapper.checkInPlanPriceStatics(0);

        if (checkInPlanPriceStatics0.size() > 0) {
            zongShouRu = checkInPlanPriceStatics0.get(0).getString("totalPlanPrice");
        }

        List<JSONObject> checkInPlanPriceStatics1 = adminChartsMapper.checkInPlanPriceStatics(1);
        if (checkInPlanPriceStatics1.size() > 0) {
            shangYueBiJiaoZongShouRu = Float.parseFloat(zongShouRu) - checkInPlanPriceStatics1.get(0).getFloat("totalPlanPrice") + "";
        }
        jsonObject.put("zongShouRu", zongShouRu);
        jsonObject.put("shangYueBiJiaoZongShouRu", shangYueBiJiaoZongShouRu);

        return jsonObject;


    }

    @Override
    public JSONObject marketDynamic(String type) {
        List<JSONObject> list = adminChartsMapper.marketDynamic();
        if ("week".equals(type)) {
            list = adminChartsMapper.marketDynamicWeek();
        }
        JSONObject jsonObject = new JSONObject();
        if("week".equals(type)){
            jsonObject.put("labels", list.stream().map(item -> item.getString("week")).toList());
            jsonObject.put("data", list.stream().map(item -> item.getString("bookingTotal")).toList());
        }else{
            jsonObject.put("labels", list.stream().map(item -> item.getString("month")).toList());
            jsonObject.put("data", list.stream().map(item -> item.getString("bookingTotal")).toList());
        }

        return jsonObject;
    }

    @Override
    public JSONObject bookingTrend(String type) {
        List<JSONObject> list = adminChartsMapper.bookingTrend();
        if ("week".equals(type)) {
            list = adminChartsMapper.bookingTrendWeek();
        }
        JSONObject jsonObject = new JSONObject();
        if("week".equals(type)){
            jsonObject.put("labels", list.stream().map(item -> item.getString("week")).toList());
            jsonObject.put("data", list.stream().map(item -> item.getString("checkPercent")).toList());
        }else {
            jsonObject.put("labels", list.stream().map(item -> item.getString("month")).toList());
            jsonObject.put("data", list.stream().map(item -> item.getString("checkPercent")).toList());
        }
        return jsonObject;
    }

    @Override
    public JSONObject roomTypePreference() {
        List<JSONObject> list = adminChartsMapper.roomTypePreference();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("labels", list.stream().map(item -> item.getString("Room_Category")).toList());
        jsonObject.put("data", list.stream().map(item -> item.getString("count")).toList());
        return jsonObject;
    }

    @Override
    public JSONObject checkInDurationDistribution() {
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> list = adminChartsMapper.checkInDurationDistribution();

        JSONObject jsonObjectLess1 = new JSONObject();
        int less1 = 0;

        JSONObject jsonObjectMore5 = new JSONObject();
        int more5 = 0;

        List<JSONObject> listResult = Lists.newArrayList();
        List<JSONObject> listNormal = Lists.newArrayList();
        for (JSONObject object : list) {
            Integer night = object.getInteger("night");
            if (night <= 1) {
                less1 += object.getInteger("cnt");
            }
            if (night >= 5) {
                more5 += object.getInteger("cnt");
            }
            if (night > 1 && night < 5) {
                listNormal.add(object);
            }


        }
        jsonObjectLess1.put("cnt", less1);
        jsonObjectLess1.put("night", "1");


        jsonObjectMore5.put("cnt", more5);
        jsonObjectMore5.put("night", "5+");


        listResult.add(jsonObjectLess1);
        listResult.addAll(listNormal);
        listResult.add(jsonObjectMore5);

        jsonObject.put("labels", listResult.stream().map(item -> item.getString("night")).toList());
        jsonObject.put("data", listResult.stream().map(item -> item.getString("cnt")).toList());


        return jsonObject;
    }

    @Override
    public JSONObject popularCheckInDate() {
        List<JSONObject> list = adminChartsMapper.popularCheckInDate();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("days", list);
        return jsonObject;
    }

    @Override
    public JSONObject staticBusinessScore(String type) {
        List<JSONObject> list = adminChartsMapper.staticBusinessScoreLineDataDay();
        if ("week".equals(type)) {
            list = adminChartsMapper.staticBusinessScoreLineDataWeek();
        } else if ("month".equals(type)) {
            list = adminChartsMapper.staticBusinessScoreLineDataMonth();
        } else {
            list = adminChartsMapper.staticBusinessScoreLineDataDay();
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("labels", list.stream().map(item -> item.getString("staticDataDay")).toList());
        jsonObject.put("totalScore", list.stream().map(item -> item.getString("totalScore")).toList());
        jsonObject.put("adminScore", list.stream().map(item -> item.getString("adminScore")).toList());
        jsonObject.put("userScore", list.stream().map(item -> item.getString("userScore")).toList());
        return jsonObject;
    }

    @Override
    public JSONObject staticBusinessScoreList(String type, String search, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> list = adminChartsMapper.staticBusinessScoreList(type, search);
        PageInfo<JSONObject> page = new PageInfo<>( list);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", page.getList());
        jsonObject.put("total", page.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject staticBusinessScoreDetail(String staticBusinessScoreSerialNumber) {
        List<JSONObject> list = adminChartsMapper.staticBusinessScoreDetail(staticBusinessScoreSerialNumber);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail", list.get(0));
        List<JSONObject> listTrend = adminChartsMapper.staticBusinessScoreLastIntervalDayTrend( list.get(0).getString("Merchant_Mapping_Blue_Plate_SerialNumber"), 7);
        if(!CollectionUtils.isEmpty(listTrend)){
            JSONObject trend = new JSONObject();
            trend.put("labels", listTrend.stream().map(item -> item.getString("staticDataDay")).toList());
            trend.put("totalScore", listTrend.stream().map(item -> item.getString("totalScore")).toList());
            jsonObject.put("trend", trend);
        }else {
            jsonObject.put("trend", new JSONObject());
        }

        return jsonObject;
    }

    @Override
    public JSONObject messageAnalysisReport() {
        JSONObject jsonObject = new JSONObject();
        int currentMonthMessageTotal = adminChartsMapper.getCurrentMonthMessageTotal();
        int lastMonthMessageTotal = adminChartsMapper.getLastMonthMessageTotal();
        BigDecimal currentMonthMessagePercent = adminChartsMapper.getCurrentMonthMessagePercent();
        BigDecimal lastMonthMessagePercent = adminChartsMapper.getLastMonthMessagePercent();
        BigDecimal currentMonthMessageAvgSatisfaction = adminChartsMapper.getCurrentMonthMessageAvgSatisfaction();
        BigDecimal lastMonthMessageAvgSatisfaction = adminChartsMapper.getLastMonthMessageAvgSatisfaction();
        int currentMonthMessageReply = adminChartsMapper.getCurrentMonthMessageReply();
        int lastMonthMessageReply = adminChartsMapper.getLastMonthMessageReply();

        jsonObject.put("currentMonthMessageTotal", currentMonthMessageTotal);
        jsonObject.put("compareLastMonthMessageTotal", currentMonthMessageTotal-lastMonthMessageTotal);

        jsonObject.put("currentMonthMessagePercent", currentMonthMessagePercent);
        jsonObject.put("compareLastMonthMessagePercent", currentMonthMessagePercent.subtract(lastMonthMessagePercent));

        jsonObject.put("currentMonthMessageAvgSatisfaction", currentMonthMessageAvgSatisfaction);
        jsonObject.put("compareLastMonthMessageAvgSatisfaction", currentMonthMessageAvgSatisfaction.subtract(lastMonthMessageAvgSatisfaction));

        jsonObject.put("currentMonthMessageReply", currentMonthMessageReply);
        jsonObject.put("compareLastMonthMessageReply", currentMonthMessageReply-lastMonthMessageReply);


        return jsonObject;
    }

    @Override
    public JSONObject messageTrend(String type) {
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> list = adminChartsMapper.messageTrend(type);
        jsonObject.put("labels", list.stream().map(item -> item.getString("name")).toList());
        jsonObject.put("data1", list.stream().map(item -> item.getString("value1")).toList());
        jsonObject.put("data2", list.stream().map(item -> item.getString("value2")).toList());
        return jsonObject;
    }

    @Override
    public JSONObject hotTopic() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", adminChartsMapper.hotTopic());
        return jsonObject;
    }

    @Override
    public JSONObject satisfactionAnalysis() {
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> list = adminChartsMapper.satisfactionAnalysis();
        jsonObject.put("labels", list.stream().map(item -> item.getString("name")).toList());
        jsonObject.put("data", list.stream().map(item -> item.getString("value")).toList());
        return jsonObject;
    }

    @Override
    public JSONObject ticketSystemVisualizationMetrics() {
        JSONObject jsonObject = new JSONObject();
        int currentDaySpotPersonCnt = adminChartsMapper.getCurrentDaySpotPersonCnt();
        int currentDaySpotTicketCnt = adminChartsMapper.getCurrentDaySpotTicketCnt();
        int currentDayBookingCnt = adminChartsMapper.getCurrentDayBookingCnt();
        int currentDayCheckInCnt = adminChartsMapper.getCurrentDayCheckInCnt();
        jsonObject.put("currentDaySpotPersonCnt", currentDaySpotPersonCnt);
        if (currentDaySpotTicketCnt > 0) {
            jsonObject.put("currentDaySpotTicketPercent", BigDecimal.valueOf(currentDaySpotPersonCnt).divide(BigDecimal.valueOf(currentDaySpotTicketCnt), 2, RoundingMode.HALF_UP));
        } else {
            jsonObject.put("currentDaySpotTicketPercent", 0);
        }
        jsonObject.put("currentDayBookingCnt", currentDayBookingCnt);
        if (currentDayBookingCnt > 0) {
            jsonObject.put("currentDayCheckInPercent", BigDecimal.valueOf(currentDayCheckInCnt).divide(BigDecimal.valueOf(currentDayBookingCnt), 2, RoundingMode.HALF_UP));
        } else {
            jsonObject.put("currentDayCheckInPercent", 0);
        }
        return jsonObject;
    }

    @Override
    public JSONObject spotRealTimeTrafficTrend() {
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> list = adminChartsMapper.getSpotRealTimeTrafficTrend();
        jsonObject.put("labels", list.stream().map(item -> item.getString("name")).toList());
        jsonObject.put("data1", list.stream().map(item -> item.getString("value")).toList());
        return jsonObject;
    }

    @Override
    public JSONObject dailyBookingVsActualVisitsComparison() {
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> list = adminChartsMapper.getDailyBookingVsActualVisitsComparison();
        jsonObject.put("labels", list.stream().map(item -> item.getString("name")).toList());
        jsonObject.put("data1", list.stream().map(item -> item.getString("value1")).toList());
        jsonObject.put("data2", list.stream().map(item -> item.getString("value2")).toList());
        return jsonObject;
    }

    @Override
    public JSONObject weeklyBookingVsActualVisitsComparison() {
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> list = adminChartsMapper.getWeeklyBookingVsActualVisitsComparison();
        jsonObject.put("labels", list.stream().map(item -> item.getString("name")).toList());
        jsonObject.put("data1", list.stream().map(item -> item.getString("value1")).toList());
        jsonObject.put("data2", list.stream().map(item -> item.getString("value2")).toList());
        return jsonObject;
    }

    @Override
    public JSONObject passengerFlowDistributionAnalysis() {
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> list = adminChartsMapper.getPassengerFlowDistributionAnalysis();
        jsonObject.put("labels", list.stream().map(item -> item.getString("name")).toList());
        jsonObject.put("data1", list.stream().map(item -> item.getString("value")).toList());
        return jsonObject;
    }
}
